package com.jinwei.qunguanjia.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.system.dao.SysOperationLogDAO;
import com.jinwei.qunguanjia.system.dto.SysOperationLogDTO;
import com.jinwei.qunguanjia.system.po.SysOperationLogPO;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 操作日志 服务层实现类
 * @author yuanzs
 */
@Service("sysOperationLogService")
public class SysOperationLogServiceImpl implements SysOperationLogService {

    @Resource
    private SysOperationLogDAO sysOperationLogDAO;

    @Override
    @Async
    public void save(SysOperationLogDTO sysOperationLogDTO) {
        SysOperationLogPO sysOperationLogPO = new SysOperationLogPO();
        BeanUtils.copyProperties(sysOperationLogDTO, sysOperationLogPO);
        sysOperationLogPO.setOperationId(IdUtils.getWorkerId());
        sysOperationLogPO.setOperationTime(DateUtils.getDateTime());
        sysOperationLogDAO.insert(sysOperationLogPO);
    }

    @Override
    public PageDataInfo<SysOperationLogDTO> listPage(SysOperationLogDTO sysOperationLogDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysOperationLogDTO);
        // 拼接查询条件
        QueryWrapper<SysOperationLogPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<SysOperationLogPO> list = sysOperationLogDAO.selectList(queryWrapper);
        PageInfo<SysOperationLogPO> pageInfo = new PageInfo<>(list);
        PageDataInfo<SysOperationLogDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, SysOperationLogDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        return pageDataInfo;
    }
}
